// 表单校验规则由 schema2code 生成，不建议直接修改校验规则，而建议通过 schema2code 生成, 详情: https://uniapp.dcloud.net.cn/uniCloud/schema


const validator = {
  "book_name": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      },
      {
        "minLength": 1,
        "maxLength": 50
      }
    ],
    "title": "书籍名称",
    "label": "书籍名称"
  },
  "authors": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      },
      {
        "minLength": 1,
        "maxLength": 50
      }
    ],
    "title": "作者",
    "label": "作者"
  },
  "publisher": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      },
      {
        "minLength": 1,
        "maxLength": 50
      }
    ],
    "title": "出版社",
    "label": "出版社"
  },
  "classify": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      },
      {
        "range": [
          {
            "text": "小说",
            "value": "novel"
          },
          {
            "text": "科技",
            "value": "technology"
          },
          {
            "text": "生活",
            "value": "life"
          },
          {
            "text": "社科",
            "value": "society"
          },
          {
            "text": "经济",
            "value": "economic"
          },
          {
            "text": "传记",
            "value": "biography"
          },
          {
            "text": "历史",
            "value": "history"
          },
          {
            "text": "儿童",
            "value": "children"
          }
        ]
      }
    ],
    "title": "分类",
    "label": "分类"
  },
  "book_cover": {
    "rules": [
      {
        "format": "file"
      }
    ],
    "title": "封面",
    "label": "封面"
  },
  "count": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "int"
      },
      {
        "minimum": 0
      }
    ],
    "title": "余量",
    "label": "余量"
  },
  "price": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "int"
      },
      {
        "minimum": 0
      }
    ],
    "title": "单价(分)",
    "label": "单价(分)"
  },
  "brief": {
    "rules": [
      {
        "format": "string"
      },
      {
        "minLength": 3,
        "maxLength": 200
      }
    ],
    "title": "简介",
    "label": "简介"
  },
  "addtime": {
    "rules": [
      {
        "format": "timestamp"
      }
    ],
    "title": "购置时间",
    "label": "购置时间"
  }
}

const enumConverter = {
  "classify_valuetotext": {
    "novel": "小说",
    "technology": "科技",
    "life": "生活",
    "society": "社科",
    "economic": "经济",
    "biography": "传记",
    "history": "历史",
    "children": "儿童"
  }
}

function filterToWhere(filter, command) {
  let where = {}
  for (let field in filter) {
    let { type, value } = filter[field]
    switch (type) {
      case "search":
        if (typeof value === 'string' && value.length) {
          where[field] = new RegExp(value)
        }
        break;
      case "select":
        if (value.length) {
          let selectValue = []
          for (let s of value) {
            selectValue.push(command.eq(s))
          }
          where[field] = command.or(selectValue)
        }
        break;
      case "range":
        if (value.length) {
          let gt = value[0]
          let lt = value[1]
          where[field] = command.and([command.gte(gt), command.lte(lt)])
        }
        break;
      case "date":
        if (value.length) {
          let [s, e] = value
          let startDate = new Date(s)
          let endDate = new Date(e)
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
      case "timestamp":
        if (value.length) {
          let [startDate, endDate] = value
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
    }
  }
  return where
}

export { validator, enumConverter, filterToWhere }
